<?php
    require_once(APPPATH . '/controllers/test/Toast.php');

    class Realestate_default_data extends Toast
    {

        function __construct()
        {
            parent::__construct(__FILE__);

            //Load default data from config files
            $this->load->config('data_object_types');
            $this->load->config('data_groups_id');
            $this->load->config('data_groups');
            $this->load->config('data_object_groups');
            $this->load->config('data_attributes');
            $this->load->config('data_group_attributes');
            $this->load->config('data_destinations'); 
        }

        public function test_models_exists()
        {
            // check if model exists
            $model = 'objects_type_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
            }

            // check if model exists
            $model = 'groups_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
                $this->_assert_true(TRUE);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
                return;
            }

            // check if model exists
            $model = 'attributes_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
                return;
            }

            // check if model exists
            $model = 'object_groups_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
                return;
            }

            // check if model exists
            $model = 'group_attributes_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
                return;
            }

            // check if model exists
            $model = 'destinations_model';
            if(file_exists(APPPATH."models/$model.php")){
                $this->load->model($model);
            }
            else{
                // model doesn't exist
                $this->_assert_true(FALSE);
                return;
            }
        }

        public function test_objects_type()
        {
            // if not rows in table, insert default data from config
            $object_types = $this->objects_type_model->find_all();
            $count = count($object_types);
            if($count>0)
            {
                //$this->firephp->log($object_types);
                for($i=0;$i<$count;$i++)
                {
                    $config_data = config_item('data_object_types');
                    $this->_assert_equals($config_data[$i]['ID'],$object_types[$i]['ID']);
                    $this->_assert_equals($config_data[$i]['type'],$object_types[$i]['type']);
                    //$this->firephp->log($config_data[$i]['type']);
                    //$this->firephp->log($object_types[$i]['type']);
                }
            }else
            { 
                foreach (config_item('data_object_types') as $row):
                    $inser_id = $this->objects_type_model->insert($row);
                    $this->firephp->log($inser_id);
                    $this->_assert_true($inser_id); // true*/
                    endforeach;
            }
        }

        public function test_groups()
        {
            // if not rows in table, insert default data from config
            $groups = $this->groups_model->find_all();
            $count = count($groups);
            if($count>0)
            {
                //$this->firephp->log($groups);
                for($i=0;$i<$count;$i++)
                {
                    $config_data = config_item('data_groups');
                    $this->_assert_equals($config_data[$i]['ID'],$groups[$i]['ID']);
                    $this->_assert_equals($config_data[$i]['name'],$groups[$i]['name']);
                    //$this->firephp->log($config_data[$i]['name']);
                    //$this->firephp->log($groups[$i]['name']);
                }
            }else
            { 
                foreach (config_item('data_groups') as $row):
                    $inser_id = $this->groups_model->insert($row);
                    $this->firephp->log($inser_id);
                    $this->_assert_true($inser_id); // true*/
                    endforeach;
            }
        }   

        public function test_object_groups()
        {
            // if not rows in table, insert default data from config
            $object_groups = $this->object_groups_model->find_all('object_type_ID');
            $count = count($object_groups);
            if($count>0)
            {
                //$this->firephp->log($object_groups);
                for($i=0;$i<$count;$i++)
                {
                    $config_data = config_item('data_object_groups');
                    $this->_assert_equals($config_data[$i]['object_type_ID'],$object_groups[$i]['object_type_ID']);
                    $this->_assert_equals($config_data[$i]['groups_ID'],$object_groups[$i]['groups_ID']);
                    //$this->firephp->log($config_data[$i]['type']);
                    //$this->firephp->log($object_groups[$i]['type']);
                }
            }else
            { 
                foreach (config_item('data_object_groups') as $row):
                    $inser_id = $this->object_groups_model->insert($row);
                    $this->firephp->log($inser_id);
                    $this->_assert_true($inser_id); // true*/
                    // description to the unit test results page:
                    $this->message = 'object_groups table have group unique key without auto increment column. insert_id return 0';
                    endforeach;
            }
        }

        public function test_attributes()
        {
            // if not rows in table, insert default data from config
            $attributes = $this->attributes_model->find_all();
            $count = count($attributes);
            if($count>0)
            {
                //$this->firephp->log($attributes);
                for($i=0;$i<$count;$i++)
                {
                    $config_data = config_item('data_attributes');
                    $this->_assert_equals($config_data[$i]['ID'],$attributes[$i]['ID']);
                    $this->_assert_equals($config_data[$i]['name'],$attributes[$i]['name']);
                    $this->_assert_equals($config_data[$i]['type'],$attributes[$i]['type']);
                    //$this->firephp->log($config_data[$i]['name']);
                    //$this->firephp->log($attributes[$i]['name']);
                }
            }else
            { 
                foreach (config_item('data_attributes') as $row):
                    $inser_id = $this->attributes_model->insert($row);
                    $this->firephp->log($inser_id);
                    $this->_assert_true($inser_id); // true*/
                    endforeach;
            }
        }

        public function test_group_attributes()
        {
            // if not rows in table, insert default data from config
            $group_attributes = $this->group_attributes_model->find_all('attributes_ID');
            $count = count($group_attributes);
            if($count>0)
            {
                //$this->firephp->log($attributes);
            }else
            { 

                $attributes = config_item('data_attributes');

                foreach (config_item('data_group_attributes') as $object_type_groups):
                    foreach ($object_type_groups as $key=>$value):

                    // Get groups_ID
                    $groups_ID = config_item($key);

                    $for_type = count($value);

                    switch ($for_type) {

                        case 2:
                            //$this->firephp->log($key.":".$groups_ID." from: ".$value[0]." to ". $value[1]);
                            //$this->firephp->log($value);

                            $from = $value[0]-1;
                            $to = $value[1]-1;
                            for($i=$from;$i<=$to;$i++){

                                //$this->firephp->log($attributes[$i]['ID'].": ".$attributes[$i]['name']);

                                $inser_id = $this->group_attributes_model->insert(array(
                                'groups_ID'     =>$groups_ID,
                                'attributes_ID' =>$attributes[$i]['ID']
                                ));
                                $this->firephp->log($inser_id);
                                $this->_assert_true($inser_id);
                                // description to the unit test results page:
                                $this->message = 'group_attributes table have group unique key without auto increment column. insert_id return 0';

                            }

                            break;

                        default:
                            $this->firephp->log($key.":".$groups_ID);
                            $this->firephp->log($value);
                            foreach ($value as $element) {

                                $this->firephp->log($attributes[$element-1]['ID']);

                                $inser_id = $this->group_attributes_model->insert(array(
                                'groups_ID'     =>$groups_ID,
                                'attributes_ID' =>$attributes[$element-1]['ID']
                                ));
                                $this->firephp->log($inser_id);
                                $this->_assert_true($inser_id);
                                // description to the unit test results page:
                                $this->message = 'group_attributes table have group unique key without auto increment column. insert_id return 0';

                            }

                    }

                    endforeach;
                    endforeach;

            }
        }

        public function test_destinations()
        {
            // if not rows in table, insert default data from config
            $detinations = $this->destinations_model->find_all();
            $count = count($detinations);
            if($count>0)
            {
                //$this->firephp->log($groups);
                for($i=0;$i<$count;$i++)
                {
                    $config_data = config_item('data_destinations');
                    $this->_assert_equals($config_data[$i]['ID'],$detinations[$i]['ID']);
                    $this->_assert_equals($config_data[$i]['name'],$detinations[$i]['name']);
                    //$this->firephp->log($config_data[$i]['name']);
                    //$this->firephp->log($groups[$i]['name']);
                }
            }else
            { 
                foreach (config_item('data_destinations') as $row):
                    $inser_id = $this->destinations_model->insert($row);
                    $this->firephp->log($inser_id);
                    $this->_assert_true($inser_id); // true*/
                    endforeach;
            }
        }
    }
?>
